import java.util.HashMap;
import java.util.Map;

public class Solution {
    // 替换所有的问号
    public String modifyString(String s) {
        int n = s.length();
        StringBuilder stringBuilder = new StringBuilder();
        for(int i = 0; i < n; i++) {
            if (s.charAt(i) == '?') {
                for(char ch = 'a'; ch <= 'z'; ch++) {
                    if((i - 1 < 0 || stringBuilder.charAt(i - 1) != ch) && (i + 1 >= n || s.charAt(i + 1) != ch)) {
                        stringBuilder.append((char)ch);
                        break;
                    }
                }
            } else {
                stringBuilder.append(s.charAt(i));
            }
        }
        return stringBuilder.toString();
    }

    // 提莫攻击
    public int findPoisonedDuration(int[] timeSeries, int duration) {
        int time = 0;
        for(int i = 1; i < timeSeries.length; i++) {
            if (timeSeries[i - 1] + duration < timeSeries[i]) {
                time += duration;
            } else {
                time += (timeSeries[i] - timeSeries[i - 1]);
            }
        }
        time += duration;
        return time;
    }

    // z 字形变换
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }
        StringBuilder stringBuilder = new StringBuilder();
        int d = 2 * numRows - 2;
        for(int i = 0; i < numRows; i++) {
            if (i == 0 || i == numRows - 1) {
                for(int j = i; j < s.length(); j += d) {
                    stringBuilder.append(s.charAt(j));
                }
            } else {
                for(int j = i, k = d - j; j < s.length() || k < s.length(); j += d, k += d) {
                    if (j < s.length()) {
                        stringBuilder.append(s.charAt(j));
                    }
                    if (k < s.length()) {
                        stringBuilder.append(s.charAt(k));
                    }
                }
            }
        }
        return stringBuilder.toString();
    }

    // 外观数列
    public String countAndSay(int n) {
        if (n == 1) {
            return "1";
        }
        StringBuilder res = new StringBuilder("11");
        for(int i = 3; i <= n; i++) {
            res = fun(res);
        }
        return res.toString();
    }

    public StringBuilder fun(StringBuilder tmp) {
        StringBuilder res = new StringBuilder();
        int i = 1;
        int num = tmp.charAt(0);
        int count = 1;
        while (i < tmp.length()) {
            while (i < tmp.length() && tmp.charAt(i) == num) {
                i++;
                count++;
            }
            res.append(count);
            res.append((num - '0'));

            if (i < tmp.length()) {
                num = tmp.charAt(i);
                count = 0;
            }
        }
        return res;
    }

    // 数青蛙
    public int minNumberOfFrogs(String croakOfFrogs) {
        int n = croakOfFrogs.length();
        Map<Character, Integer> index = new HashMap<>();
        String t = "croak";
        int len = t.length();
        for(int i = 0; i < len; i++) {
            index.put(t.charAt(i), i);
        }
        int[] hash = new int[len];
        for(char ch: croakOfFrogs.toCharArray()) {
            if (ch == t.charAt(0)) {
                if (hash[len - 1] != 0) {
                    hash[len - 1]--;
                }
                hash[0]++;
            } else {
                int i = index.get(ch);
                if (hash[i - 1] == 0) {
                    return -1;
                } else {
                    hash[i - 1]--;
                    hash[i]++;
                }
            }
        }

        for(int i = 0; i < len - 1; i++) {
            if (hash[i] != 0) {
                return -1;
            }
        }
        return hash[len - 1];
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
//        solution.convert("A", 1);
//        System.out.println(solution.countAndSay(4));
        System.out.println(solution.minNumberOfFrogs("crcoakroak"));
    }
}
